package com.freshcity.page.welcome
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.welcome.FriendCondWin;
	import com.greensock.TweenMax;
	import com.greensock.loading.LoaderMax;
	
	import fl.controls.ComboBox;
	import fl.data.DataProvider;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class FriendCond extends Component
	{
		private var back:FriendCondWin;
		private var propertyItem2:ComboBox;
		private var propertyItem1:ComboBox;
		private var provinceBox:ComboBox;

		private var xml:XML=LoaderMax.getContent("dataXML");

		public function FriendCond(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(LayerManager.popupLayer, xpos, ypos);
			this.x=(stage.stageWidth - back.width) / 2;
			this.y=(stage.stageHeight - back.height) / 2;

			back.submitBtn.addEventListener(MouseEvent.CLICK, onSubmitClickHandler);
		}

		protected function onSubmitClickHandler(event:MouseEvent):void
		{
			var value1:String;

			if (back.fromAge.text.length < 2 || back.toAge.text.length < 2)
			{
				main.msg.showRollTipsMsg("请选择年龄范围");
				return;
			}else if(int(back.fromAge.text)>=int(back.toAge.text)){
				main.msg.showRollTipsMsg("终止年龄必须大于起始年龄");
				return;
			}

			if(int(back.fromTall.selectedLabel)>=int(back.toTall.selectedLabel)){
				main.msg.showRollTipsMsg("终止身高必须大于起始身高");
				return;
			}
			
			if (propertyItem1.selectedIndex > 0)
			{
				value1=provinceBox.selectedLabel + propertyItem1.selectedLabel;
			}
			else
			{
				value1=provinceBox.selectedLabel;
			}

			var data:*={};
			data.ageFrom=back.fromAge.text;
			data.ageTo=back.toAge.text;
			data.addProv = provinceBox.selectedIndex > 0 ? provinceBox.selectedLabel:'';
			data.addCity = propertyItem1.selectedIndex > 0 ? propertyItem1.selectedLabel:'';
			data.tallFrom = back.fromTall.selectedLabel;
			data.tallTo = back.toTall.selectedLabel;
			data.degree = back.degreeCombox.selectedIndex > 0 ? back.degreeCombox.selectedLabel:'';

			main.commu.send(OpCodes.SETTING_QQ_FRIEND, data, onSettingCallback);
		}
		
		private function onSettingCallback(data:*):void
		{
			if(data is String){
				main.msg.showRollTipsMsg(data);
			}else{
				main.cond = data;
				main.msg.showRollTipsMsg("成功完成操作");
				if(main.settingCallback!=null){
					main.settingCallback();
					this.destroy();
				}else{
					this.hide();
				}
			}
		}		

		override protected function addChildren():void
		{
			super.addChildren();
			back=new FriendCondWin();
			back.mouseEnabled=false;
			addChild(back);

			provinceBox=back.provinceCombox;
			var da:Array=[];
			da.push({label: '不限'});
			for each (var x:XML in xml.cities.prov)
			{
				da.push({label: x.@label});
			}
			provinceBox.dataProvider=new DataProvider(da);
			provinceBox.addEventListener(Event.CHANGE, provinceSelected);
			provinceBox.selectedIndex=0;
			
			//居住地
			propertyItem1=back.cityCombox;

			//身高
			var tallArray:Array=[];
			for each (x in xml.tall.children())
			{
				tallArray.push({label: x.@value});
			}
			back.fromTall.dataProvider=new DataProvider(tallArray);
			back.fromTall.selectedIndex=25;
			back.toTall.dataProvider=new DataProvider(tallArray);
			back.toTall.selectedIndex=50;

			//学历
			var degreeArray:Array=[];
			degreeArray.push({label: '不限'});
			for each (x in xml.degree.children())
			{
				degreeArray.push({label: x.@value});
			}
			back.degreeCombox.dataProvider=new DataProvider(degreeArray);
			back.degreeCombox.selectedIndex=0;
		}

		private function provinceSelected(e:Event):void
		{
			var da:Array=[];
			var c:ComboBox=e.currentTarget as ComboBox;
			da.push({label: '不限'});
			for each (var x:XML in xml.cities.prov.(@label == c.selectedItem.label).children())
			{
				da.push({label: x.@value});
			}
			propertyItem1.dataProvider=new DataProvider(da);
			propertyItem1.selectedIndex=0;
		}

		override public function destroy():void
		{
			back.submitBtn.removeEventListener(MouseEvent.CLICK, onSubmitClickHandler);
			super.destroy();
		}

		override public function show(event:Event=null):void
		{
			EffectManager.addUIMask([],0);
			super.show();
		}

		override public function hide(event:Event=null):void
		{
			EffectManager.hideUIMask();
			super.hide(event);
		}
	}
}
